$().ready(function() {

});
function topuser(){
	owner = $.cookie('owner');
	strImgLink = $.cookie('strImgLink');
	html="";
	html+='<div class="user-info-box">';
		html+='<div class="wrapper-user-info">';
			html+='<div class="user-info">';
				html+='<i class="user-info-label">I am...</i>';
				html+='<i class="user-info-name">'+owner+'</i>';
			html+='</div>';
			html+='<div class="user-info-photo-box img-rounded">';
				html+='<i class="user-info-photo img-rounded"><img class="img-rounded" src="'+base_url+"assets/uploaded/user_img/thumbnail/"+strImgLink+'" /></i>';
			html+='</div>';
		html+='</div>';
		html+='<i class="clearfix"></i>';
		html+='<div class="user-info-points-wrapper img-rounded">';
			html+='<div class="user-info-points">';
				html+='<ul>';
				html+='</ul>';
			html+='</div>';
		html+='</div>';
		html+='<i class="clearfix"></i>';
		html+='<div class="user-points-credits-box">';
			html+='<div class="wrapper-user-points-credits img-rounded">';
				html+='<i class="user-points-credits img-rounded"></i>';
			html+='</div>';
			html+='<div class="wrapper-user-points-earn-credits img-rounded">';
				html+='<a><i class="user-points-earn-credits img-rounded">Earn Credits</i></a>';
			html+='</div>';
		html+='</div>';
		html+='<div class="earn-credit"></div>';
	html+='</div>';
	$('.box-two .title span').text("Top User");
	$('.box-two-content').html(html);
	html='<div class="top-user-btn pull-right" title="Top Rank"><a></a></div>';
	$('.box-two .title').append(html);	
	$('.user-info-photo-box').rotate(15);
	get_user_data();
	set_top_user_fileupload();
	switchwindow('topuser');	
	$('.user-points-earn-credits').click(function(){
		$( ".earn-credit" ).dialog({
			title: "Earn Credits",
			autoOpen: false,
			minHeight: 140,
			modal: true,
			draggable: false ,
			show: {
				effect: "explode",
				duration: 300
			},
			hide: {
				effect: "explode",
				duration: 300
			},	      
			buttons: {
					"Ok": function() {
					update_user_points_credit();
					$(this).dialog("close");
					$('.earn-credit').remove();
					$('.user-info-box').append('<div class="earn-credit"></div>');
				},			
				"Cancel": function() {
					$(this).dialog("close");
					$('.earn-credit').remove();
					$('.user-info-box').append('<div class="earn-credit"></div>');				
				}
			}
		});		
		get_con_points();
		$(".earn-credit").dialog("open");
	});
	$('.top-user-btn').click(function (){
		show_top_rank();
	});
}
function list_top_rank(){

	xfunc = "topuser/list_topuser";
	data_sent = {}
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request

			html='';
			html+='<li>';
				html+='<ul>';
					html+='<li>';
						html+='<i class="rank-label">&nbsp;Rankings</i>';
						html+='<i class="points-label">Points</i>';
						html+='<i class="clearfix"></i>';
					html+='</li>';
				html+='</ul>';
			html+='</li>';			
			
			inc=1;
			$(result.intID).each(function(k,v){
				html+='<li>';
					html+='<ul class="top-border">';
						html+='<li>';
							html+='<i class="rank-no rank-text">'+inc+'</i>';
							html+='<i class="top-user-list-rank-photo img-rounded">';
								html+='<i class="rank-photo"><img class="img-rounded" src="'+base_url+"assets/uploaded/user_img/thumbnail/"+result.strImgLink[k]+'" /></i>';
							html+='</i>';
							html+='<i class="rank-name rank-text">'+result.strUserName[k]+'</i>';
							html+='<i class="rank-points rank-text">'+result.intPoints[k]+'</i>';
							html+='<i class="clearfix"></i>';
						html+='</li>';
					html+='</ul>';
				html+='</li>';
				inc++;
			});
			$('.top-user-list-rank ul').html(html);
		}
	});	
}
function show_top_rank(){
	html='';
	
	html+='<div class="top-user-list-rank-box">';
		html+='<div class="wrapper-top-user-list-rank img-rounded">';
			html+='<div class="top-user-list-rank img-rounded">';
				html+='<ul>';						
				html+='</ul>';
			html+='</div>';
		html+='</div>';
	html+='</div>';
	html+='<i class="clearfix"></i>';
	$('.box-two .title span').text("Top User");
	$('.box-two-content').html(html);
	$('.top-user-btn').remove();
	html='<div class="back-btn-two pull-right" title="Top User"><a></a></div>';
	$('.box-two .title').append(html);
	list_top_rank();
	$('.back-btn-two').click(function(){
		$('.back-btn-two').remove();
		owner = $.cookie('owner');
		strImgLink = $.cookie('strImgLink');
		html="";
		html+='<div class="user-info-box">';
			html+='<div class="wrapper-user-info">';
				html+='<div class="user-info">';
					html+='<i class="user-info-label">I am...</i>';
					html+='<i class="user-info-name">'+owner+'</i>';
				html+='</div>';
				html+='<div class="user-info-photo-box img-rounded">';
					html+='<i class="user-info-photo img-rounded"><img class="img-rounded" src="'+base_url+"assets/uploaded/user_img/thumbnail/"+strImgLink+'" /></i>';
				html+='</div>';
			html+='</div>';
			html+='<i class="clearfix"></i>';
			html+='<div class="user-info-points-wrapper img-rounded">';
				html+='<div class="user-info-points">';
					html+='<ul>';
					html+='</ul>';
				html+='</div>';
			html+='</div>';
			html+='<i class="clearfix"></i>';
			html+='<div class="user-points-credits-box">';
				html+='<div class="wrapper-user-points-credits img-rounded">';
					html+='<i class="user-points-credits img-rounded"></i>';
				html+='</div>';
				html+='<div class="wrapper-user-points-earn-credits img-rounded">';
					html+='<a><i class="user-points-earn-credits img-rounded">Earn Credits</i></a>';
				html+='</div>';
			html+='</div>';
			html+='<div class="user-points-upload-photo">';
				html+='<a><i class="user-points-earn-change-photo">Change Photo</i></a>';
			html+='</div>';
			html+='<div class="earn-credit"></div>';
		html+='</div>';
		$('.box-two .title span').text("Top User");
		$('.box-two-content').html(html);
		html='<div class="top-user-btn pull-right" title="Top Rank"><a></a></div>';
		$('.box-two .title').append(html);	
		$('.user-info-photo-box').rotate(15);
		get_user_data();
		$('.user-points-earn-credits').click(function(){
			$( ".earn-credit" ).dialog({
				title: "Earn Credits",
				autoOpen: false,
				minHeight: 140,
				modal: true,
				draggable: false ,
				show: {
					effect: "explode",
					duration: 300
				},
				hide: {
					effect: "explode",
					duration: 300
				},	      
				buttons: {
						"Ok": function() {
						update_user_points_credit();
						$(this).dialog("close");
						$('.earn-credit').remove();
						$('.user-info-box').append('<div class="earn-credit"></div>');
					},			
					"Cancel": function() {
						$(this).dialog("close");
						$('.earn-credit').remove();
						$('.user-info-box').append('<div class="earn-credit"></div>');				
					}
				}
			});		
			get_con_points();
			$(".earn-credit").dialog("open");
		});
		$('.top-user-btn').click(function (){
			show_top_rank();
		});
	});
}
function update_user_points_credit(){
	var ape = $('.available-points-eq').text();
	var pe = $('.points-eq').text();
	var credit = $(".get-credit option[value='"+pe+"']").text()
	var old_credit = $('.credit-class').text();
	var intAvailPoints = ape*1-pe*1;
	var intCredit = credit*1+old_credit*1
	var intUserID = $.cookie('session_code');
	var ec = $( ".earn-credit" ).text();
	if($.trim(ec) != "Not enough Points"){
		$('.ape-class').text(intAvailPoints);
		$('.credit-class').text(intCredit);
		var intUserID = $.cookie('session_code');
	
		xfunc = "users/update_user_points";
		data_sent = {
				'intAvailPoints':intAvailPoints,
				'intCredit':intCredit,
				'intUserID':intUserID
		};
		
		$.ajax({ 
			type: "POST", // request type POST
			url: base_url+xfunc, // post to PHP file
			data: data_sent, // request send to PHP file
			dataType:'json',
			success: function(result){ // result = return from php request
				$('.user-info-box').append('<div class="result-message-box"></div>');
			    $( ".result-message-box" ).dialog({
			    	title: "Save Check List",
			    	autoOpen: false,
			    	minHeight: 140,
					modal: true,
					draggable: false ,
					show: {
						effect: "explode",
						duration: 300
					},
					hide: {
						effect: "explode",
						duration: 300
					},	      
					buttons: {
						"Ok": function() {
							$(this).dialog("close");
							$( ".result-message-box" ).remove();
						}
					}
			      });
			    $('.ui-dialog-content').text(result.message);
				$( ".result-message-box" ).dialog("open");
			}
		});	
	}
}
function get_user_data(){
	var intUserID = $.cookie('session_code');

	xfunc = "users/get_user_data";
	data_sent = {
			'intID':intUserID
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request
			var strPointType = new Array();
			var intUserPoints = new Array();
			strPointType = JSON.stringify(result.strPointType);
			JSON.parse(strPointType, parse_json_strPointType);
			intUserPoints = JSON.stringify(result.intUserPoints);
			JSON.parse(intUserPoints, parse_json_intUserPoints);
			$('.user-points-credits').text(result.sysPoint+' Points = '+result.sysCredit+' Credits');		
		}
	});	
}
function get_con_points(){
	var intUserID = $.cookie('session_code');

	xfunc = "users/get_user_data";
	data_sent = {
			'intID':intUserID
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request
			sysPoint = result.sysPoint * 1;
			i=sysPoint;
			intAvailPoints = result.intAvailPoints * 1; 
			credit = result.sysCredit;
			if(sysPoint>intAvailPoints){
				html='Not enough Points';
				$( ".earn-credit" ).html(html);
			} else {
				html='<div class="box-get-credit">';
					html+='<div class="label-available-points-eq">Available Points: </div>';
					html+='<div class="available-points-eq">';
						html+=intAvailPoints;
					html+='</div>';		
					html+='<i class="clearfix"></i>';
					html+='<div class="label-points-eq">Points: </div>';
					html+='<div class="points-eq">';
						html+=i;
					html+='</div>';		
					html+='<i class="clearfix"></i>';
					html+='<div class="label-get-credit">Credit: </div>'
					html+='<select class="get-credit">';
					while (i<=intAvailPoints) {
						html+='<option value="'+i+'">'+credit+'</option>';
						credit++;				
						i+=sysPoint;
					}
					html+='</select>';
				html+='</div>';
				$( ".earn-credit" ).html(html);
				$('.get-credit').change(function(){
					$('.points-eq').text($(this).val());
				});
			}
		}
	});	
}
function parse_json_strPointType (k,v){
	var obj = '';
	if($.trim(k)!=""){
		class_ape = '';
		if(v=='Available Points'){
			class_ape = 'ape-class';
		} else if(v=='Credits'){
			class_ape = 'credit-class';
		}		
		obj+='<li class="box-bg img-rounded">';
			obj+= '<i class="points-label-info">'+v+'</i><i id="int-points_'+k+'" class="int-points '+class_ape+'"></i>';
		obj+='</li>';
		$('.user-info-points ul').append(obj);
	}
}
function parse_json_intUserPoints(k,v){
	if($.trim(k)!=""){
		$('#int-points_'+k).text(v);
	}
}